#nullable enable

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using PHMEE.DTOS.Diagnosis;
using PHMEE.Localization;
using PHMEE.Managers.Diagnosis;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Uow;

namespace PHMEE.Services.Diagnosis;

// /// <summary>
// /// 报警记录创建事件 Handler
// /// 该事件由 Admin 项目发布（因为数采程序和故障诊断程序在 Admin 项目中），所以需要分布式事件处理器
// /// </summary>
// public class AlarmRecordCreatedEventHandler : IDistributedEventHandler<AlarmRecordCreatedEto>, ITransientDependency
// {
//     private readonly ILogger<AlarmRecordCreatedEventHandler> _logger;
//     private readonly IHubContext<AlarmRecordRealtimeHub, IAlarmRecordRealtimeClient> _alarmRecordRealtimeHub;
//     private readonly IMainenanceAdviceRepository _maintenanceAdviceRepository;
//     private readonly IStringLocalizer<DiagnosisResource> _localizer;
//
//     public AlarmRecordCreatedEventHandler(ILogger<AlarmRecordCreatedEventHandler> logger,
//         IHubContext<AlarmRecordRealtimeHub, IAlarmRecordRealtimeClient> alarmRecordRealtimeHub,
//         IMainenanceAdviceRepository maintenanceAdviceRepository,
//         IStringLocalizer<DiagnosisResource> localizer)
//     {
//         _logger = logger;
//         _alarmRecordRealtimeHub = alarmRecordRealtimeHub;
//         _maintenanceAdviceRepository = maintenanceAdviceRepository;
//         _localizer = localizer;
//     }
//     
//     [UnitOfWork]
//     public async Task HandleEventAsync(AlarmRecordCreatedEto eventData)
//     {
//         _logger.LogDebug("处理新报警 Handler: {0}", eventData.Id);
//         
//         // 特征值-报警等级
//         var featureAlarmLevels = eventData.FeatureAlarmLevels
//             .Select(f => new FeatureAlarmLevelsDto
//             {
//                 FeatureValueName = f.Key.ToString(),
//                 AlarmLevel = f.Value.ToString()
//             });
//             
//         // 诊断结论和维保建议
//         var maintenanceAdvices = await _maintenanceAdviceRepository.GetListByFaultCodesAsync(eventData.FaultCodes);
//         var maintenanceInfos = maintenanceAdvices.Select(ma => new MaintenanceInfoDto
//         {
//             Type = ma.Type,
//             Advice = ma.Advice
//         }).ToArray();
//
//         var alarmRecordRealtimeDto = new AlarmRecordRealtimeDto
//         {
//             AlarmRecordId = eventData.Id,
//             AlarmRecordTimeUtc = eventData.AlarmTimeUtc,
//             PointId = eventData.PointId,
//             PointName = eventData.PointName,
//             UnitId = eventData.UnitId,
//             EquipId = eventData.EquipId,
//             FeatureAlarmLevels = featureAlarmLevels,
//             MaintenanceInfos = maintenanceInfos
//         };
//         
//         _logger.LogDebug("Group: {0}", eventData.UnitId.ToString());
//         
//         await _alarmRecordRealtimeHub.Clients.Group(eventData.UnitId.ToString()).NewAlarm(alarmRecordRealtimeDto);
//     }
// }